package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcOrderBankInfoEndorse;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.OrderBankInfoEndorseForm;
import com.yxinsur.imc.business.form.OrderEndorseForm;
import com.yxinsur.imc.business.mapper.ImcOrderBankInfoEndorseMapper;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.service.IImcOrderBankInfoEndorseService;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 保单对应银行卡缴费信息批单Service业务层处理
 *
 * @author imc
 * @date 2021-10-09
 */
@Service
public class ImcOrderBankInfoEndorseServiceImpl extends ServiceImpl<ImcOrderBankInfoEndorseMapper, ImcOrderBankInfoEndorse> implements IImcOrderBankInfoEndorseService {
    @Autowired
    private ImcOrderBankInfoEndorseMapper imcOrderBankInfoEndorseMapper;
    @Autowired
    private ImcOrderMapper imcOrderMapper;

    /**
     * 新增缴费信息批单
     *
     * @param endorseForm 缴费信息批单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult addImcOrderBankInfoEndorse(OrderEndorseForm endorseForm) {
        Long orderId = endorseForm.getOrderId();

        // 删除缴费信息
        ImcOrderBankInfoEndorse oldBankInfoEndorse = this.imcOrderBankInfoEndorseMapper.getOrderBankInfoEndorseByOrderId(orderId);
        if (ObjectUtil.isNotNull(oldBankInfoEndorse)) {
            oldBankInfoEndorse.setUpdateId(SecurityUtils.getUsername());
            oldBankInfoEndorse.setUpdateTime(DateUtil.date());
            this.imcOrderBankInfoEndorseMapper.deleteImcOrderBankInfoEndorseById(oldBankInfoEndorse);
        }

        // 保存缴费信息
        String serialId = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN);
        OrderBankInfoEndorseForm bankInfoForm = endorseForm.getBankInfoEndorseForm();
        ImcOrderBankInfoEndorse bankInfoEndorse = new ImcOrderBankInfoEndorse();
        BeanUtil.copyProperties(bankInfoForm, bankInfoEndorse, CopyOptions.create().ignoreCase().setIgnoreNullValue(true));
        bankInfoEndorse.setOrderId(orderId);
        bankInfoEndorse.setSerialId(serialId);
        bankInfoEndorse.setIsDeleted(IsDeletedEnum.N.getCode());
        bankInfoEndorse.setCreateId(SecurityUtils.getUsername());
        bankInfoEndorse.setCreateTime(DateUtil.date());
        bankInfoEndorse.setUpdateId(SecurityUtils.getUsername());
        bankInfoEndorse.setUpdateTime(DateUtil.date());
        bankInfoEndorse.setTenantId(SecurityUtils.getTenantId());
        imcOrderBankInfoEndorseMapper.insertImcOrderBankInfoEndorse(bankInfoEndorse);
        return AjaxResult.success();
    }
}
